package com.css.fxfzfxqh.modules.lifelineEngineering.repository.impl;

import com.css.fxfzfxqh.modules.lifelineEngineering.entity.LifelineTaskEntity;
import com.css.fxfzfxqh.modules.lifelineEngineering.param.FuzzyQueryParam;
import com.css.fxfzfxqh.modules.lifelineEngineering.param.PageParam;
import com.css.fxfzfxqh.modules.lifelineEngineering.repository.TaskManagementRepository;
import com.css.fxfzfxqh.util.PlatformObjectUtils;
import com.css.fxfzfxqh.util.PlatformSessionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
@Slf4j
public class TaskManagementRepositoryImpl implements TaskManagementRepository {

    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public void save(LifelineTaskEntity entity) {
        jdbcTemplate.update("insert into fxqh_lifeline_task  (id,code,name,info,evaluation_object,input_method,influence_field,create_user,create_time,del_flag,role,province,city,county,create_user_name) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,? )",
                entity.getId(), entity.getCode(), entity.getName(), entity.getInfo(), entity.getEvaluationObject(), entity.getInputMethod(), entity.getInfluenceField(), entity.getCreateUser(), entity.getCreateTime(), entity.getDelFlag(), entity.getRole(), entity.getProvince(), entity.getCity(), entity.getCounty(), entity.getCreateUserName());
    }

    @Override
    public void update(LifelineTaskEntity entity) {
        try {
            jdbcTemplate.update("UPDATE  fxqh_lifeline_task  SET code =?,name=?,info=?,evaluation_object=?,input_method=?,influence_field=?,update_user=?,update_time=?"
                            + " where id=?",
                    entity.getCode(), entity.getName(), entity.getInfo(), entity.getEvaluationObject(), entity.getInputMethod(), entity.getInfluenceField(), entity.getUpdateUser(), entity.getUpdateTime(),
                    entity.getId()
            );
        } catch (DataAccessException e) {
            log.error("update fxqh_lifeline_task error:{}", e.getMessage(), e);
        }
    }

    @Override
    public Integer getByName(String name) {
        String userId = PlatformSessionUtils.getUserId();
        String sql = "select count(1) from fxqh_lifeline_task where del_flag = '0' and name = '" + name + "' and create_user = '" + userId + "'";
        try {
            return jdbcTemplate.queryForObject(sql, Integer.class);
        } catch (DataAccessException e) {
            return 0;
        }
    }

    @Override
    public Map<String, Object> page(PageParam param, int page, int size) {
        String userId = PlatformSessionUtils.getUserId();
        StringBuilder sql = new StringBuilder("select * from fxqh_lifeline_task where del_flag = '0' and create_user = '" + userId + "'");
        if (PlatformObjectUtils.isNotEmpty(param.getCode())) {
            sql.append(" and code like '%" + param.getCode() + "%'");
        }
        if (PlatformObjectUtils.isNotEmpty(param.getName())) {
            sql.append(" and name like '%" + param.getName() + "%'");
        }
        if (PlatformObjectUtils.isNotEmpty(param.getInputMethod())) {
            sql.append(" and input_method = '" + param.getInputMethod() + "'");
        }
        if (PlatformObjectUtils.isNotEmpty(param.getEvaluationObject())) {
            sql.append(" and evaluation_object like '%" + param.getEvaluationObject() + "%'");
        }
        StringBuilder orderSql = new StringBuilder(" order by create_time desc");
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(LifelineTaskEntity.class)).size();
        StringBuilder limitSql = new StringBuilder(" limit " + size + " offset " + (page - 1) * size);
        sql.append(orderSql).append(limitSql);
        List<LifelineTaskEntity> data = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(LifelineTaskEntity.class));
        HashMap<String, Object> resultMap = new HashMap();
        resultMap.put("total", total);
        resultMap.put("rows", data);
        return resultMap;
    }

    @Override
    public LifelineTaskEntity getEntityById(String id) {
        String sql = "select * from fxqh_lifeline_task where id = '" + id + "'";
        try {
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(LifelineTaskEntity.class));
        } catch (DataAccessException e) {
            return new LifelineTaskEntity();
        }
    }

    @Override
    public void updateCurrentTask(String userId) {
        String sql = "update fxqh_lifeline_task set current_task = '0' where del_flag = '0' and current_task = '1' and create_user = '" + userId + "'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void currentTaskById(String id) {
        String sql = "update fxqh_lifeline_task set current_task = '1' where id = '" + id + "'";
        jdbcTemplate.update(sql);
    }

    @Override
    public String getCode(String userId) {
        try {
            StringBuilder sql = new StringBuilder("select code from fxqh_lifeline_task where del_flag = '0' and create_user = '" + userId + "' order by create_time desc limit 1");
            return jdbcTemplate.queryForObject(sql.toString(), String.class);
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public void deleteById(String id) {
        String sql = "update fxqh_lifeline_task set del_flag = '1' where id = '" + id + "'";
        jdbcTemplate.update(sql);
    }

    @Override
    public LifelineTaskEntity getCurrentTask(String userId) {
        String sql = "select * from fxqh_lifeline_task where current_task = '1' and create_user = '" + userId + "'";
        try {
            return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(LifelineTaskEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public Map<String, Object> fuzzyQuery(FuzzyQueryParam param, int page, int size) {
        StringBuilder sql = new StringBuilder("select * from fxqh_lifeline_task where del_flag = '0'");
        if (PlatformObjectUtils.isNotEmpty(param.getCode())) {
            if (param.getCode() == 1) {
                if (PlatformObjectUtils.isNotEmpty(param.getInfo())) {
                    sql.append(" and code like '%" + param.getInfo() + "%'");
                }
            } else if (param.getCode() == 2) {
                if (PlatformObjectUtils.isNotEmpty(param.getInfo())) {
                    sql.append(" and create_user_name like '%" + param.getInfo() + "%'");
                }
            }
        }
        StringBuilder orderSql = new StringBuilder(" order by create_time desc");
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(LifelineTaskEntity.class)).size();
        StringBuilder limitSql = new StringBuilder(" limit " + size + " offset " + (page - 1) * size);
        sql.append(orderSql).append(limitSql);
        List<LifelineTaskEntity> data = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(LifelineTaskEntity.class));
        HashMap<String, Object> resultMap = new HashMap();
        resultMap.put("total", total);
        resultMap.put("rows", data);
        return resultMap;
    }
}
